package zxq.rollcall.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import zxq.rollcall.course.entity.SystemSignTableEntity;
import zxq.rollcall.course.entity.dto.db.CourseSignDTO;
import zxq.rollcall.course.entity.dto.db.StudentInfoDTO;
import zxq.rollcall.course.entity.dto.db.StudentSignDTO;
import zxq.rollcall.course.entity.dto.db.TeacherSignDTO;
import zxq.rollcall.course.entity.query.PageQuery;
import zxq.rollcall.course.mapper.SystemSignTableDao;
import zxq.rollcall.course.service.MPSystemSignTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户签到得表 服务实现类
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Service
public class SystemSignTableServiceDiy extends ServiceImpl<SystemSignTableDao, SystemSignTableEntity> implements MPSystemSignTableService {


    @Override
    public Boolean initStudentSign(Integer teacherId, List<Integer> stuIds) {
       List<SystemSignTableEntity> signTableEntities = stuIds.stream().map(stuId -> {
            SystemSignTableEntity systemSignTableEntity = new SystemSignTableEntity();
            systemSignTableEntity.setStartSignTime(getTimeStamp());
            systemSignTableEntity.setTeacherSign(teacherId);
            systemSignTableEntity.setStudentId(stuId);
            return systemSignTableEntity;
        }).collect(Collectors.toList());
        return saveBatch(signTableEntities);
    }


    @Override
    public Boolean updateStudentSign(Integer teacherId, Integer studentId) {
       SystemSignTableEntity systemSignTableEntity = new SystemSignTableEntity();
       systemSignTableEntity.setTeacherSign(teacherId);
       systemSignTableEntity.setStudentId(studentId);
       QueryWrapper queryWrapper = new QueryWrapper(systemSignTableEntity);
       systemSignTableEntity = getOne(queryWrapper);
       systemSignTableEntity.setSign(true);
       systemSignTableEntity.setEndSignTime(getTimeStamp());
       return updateById(systemSignTableEntity);
    }

    @Override
    public List<CourseSignDTO> queryCourseSign(Integer courseId) {
        return getBaseMapper().queryCourseSign(courseId,null);
    }

    @Override
    public CourseSignDTO queryCourseTeacherSign(Integer courseId,Integer teacherSignId) {
        List<CourseSignDTO> courseSignDTOS =  getBaseMapper().queryCourseSign(courseId,teacherSignId);
        if(courseSignDTOS.size() > 0) {
            return courseSignDTOS.get(0);
        }
        return null;
    }

    @Override
    public List<StudentSignDTO> queryStudentSign(Integer courseId) {
        return getBaseMapper().queryStudentSign(courseId,null);
    }

    @Override
    public StudentSignDTO queryStudentSignDetail(Integer courseId, Integer studentId) {
        List<StudentSignDTO> studentSignDTOS = getBaseMapper().queryStudentSign(courseId,studentId);
        if(studentSignDTOS.size() > 0)
            return studentSignDTOS.get(0);
        return null;
    }

    @Override
    public List<StudentInfoDTO> queryStudentInfoSign(Integer studentId, PageQuery pageQuery) {

        return getBaseMapper().queryStudentInfoSign(studentId,null, matrix(pageQuery));
    }


    @Override
    public List<StudentInfoDTO> queryCourseStudentInfoSign(Integer studentId, Integer courseId, PageQuery pageQuery) {

        return getBaseMapper().queryStudentInfoSign(studentId,courseId, matrix(pageQuery));
    }

    @Override
    public List<TeacherSignDTO> queryTeacherSignDetail(Integer teacherSignId) {

        return getBaseMapper().queryTeacherSignDetail(teacherSignId);
    }

    /**
     * 转换分页数据
     * @param pageQuery 分页数据
     * @return
     */

    private PageQuery matrix(PageQuery pageQuery) {
        Integer count = pageQuery.getCount();
        Integer page = pageQuery.getPage();
        pageQuery.setPage((page-1)*count);
        pageQuery.setCount(count);
        return pageQuery;
    }

    private String getTimeStamp() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr = df.format(new Date());
        return timeStr;
    }



}
